package chen.bupt.vsmtest;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ansj.domain.Term;
import org.ansj.splitWord.analysis.ToAnalysis;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import chen.bupt.text.TextClear;

public class LocalTest {
	private static Logger logger = Logger.getLogger(LocalTest.class);
	private static final String featurePath = "/home/forrest/tmp/feature_sorted";
	private static final String userVectorPath = "/home/forrest/tmp/user_ballard_vector";
	private static final String dataPath = "/home/forrest/tmp/contentdata";
	private static Map<String, Integer> map = new HashMap<String, Integer>();
	private static Map<Integer, Double> userFeatureVector = new HashMap<Integer, Double>();
	private static List<String> list = new ArrayList<String>();
	private static final String user = "ballard";
	private static double userWeight = 0.0;

	public static void loadTestData() {

		logger.info("loading feature...");
		try {
			int i = 0;
			String feature = FileUtils.readFileToString(new File(featurePath));
			String[] words = feature.split(";");
			String vector[] = FileUtils.readFileToString(
					new File(userVectorPath)).split("\\|");
			for (String word : words) {
				if (!"".equals(word)) {
					map.put(word, i);
					i++;
				}
			}
			for (String v : vector) {
				userWeight += (Double.parseDouble(v.split(";")[1]) * Double
						.parseDouble(v.split(";")[1]));
				userFeatureVector.put(Integer.parseInt(v.split(";")[0]), Double
						.parseDouble(v.split(";")[1]));
			}
			userWeight = Math.sqrt(userWeight);
			list = FileUtils.readLines(new File(dataPath));
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private static class Content {
		public double score;
		public String content;

		public Content(double d, String s) {
			score = d;
			content = s;
		}

		public String toString() {
			return score + ":" + content;
		}
	}

	private static class MyCompatator implements Comparator<Content> {

		@Override
		public int compare(Content o1, Content o2) {
			return (int) (o2.score - o1.score);
		}

	}

	public static void main(String[] args) {
		List<Content> contList = new ArrayList<Content>();
		loadTestData();
		double score = 0.0;
		int score1 = 0;
		for (String content : list) {
			score = 0.0;
			score1 = 0;
			content = TextClear.clearText(content);
			if (content == null || "".equals(content))
				return;
			List<Term> terms = ToAnalysis.paser(content);

			for (Term t : terms) {
				String tt = t.getName();
				Integer ind = map.get(tt);
				if (ind != null) {
					Double d = userFeatureVector.get(ind);
					if (d != null) {
						score += d;
					}
					score1 += 1;
				}
			}
			if (score1 != 0) {
				contList.add(new Content(score/(Math.sqrt(score1*userWeight)),content));
			}
		}
		Collections.sort(contList, new MyCompatator());
		for(Content c:contList){
			System.out.println(c.toString());
		}
	}

}
